#include "main.h"
sqlite3 *db;
char *emsg;
msg_node msg;
struct epoll_event ev, events[100]; // 创建epoll结构体变量和结构体数组
int epollfd;                        // 创建一个epoll对象，
void _register(int fd)//注册
{
    reg s1 = *(reg *)msg.buf;
    dkbg();
    char sql[] = "CREATE TABLE zhanghao(name varchar(255),passwd int,position varchar(255),fd int);";
    _sqlite3_exec(db, sql);

    char sql2[128];
    sprintf(sql2, "CREATE TABLE %s聊天记录(sname varchar(255),oname varchar(255),txt varchar(255),sign int);", s1.name);
    _sqlite3_exec(db, sql2);

    if (strcmp(s1.position, "员工") == 0)
    {

        sprintf(sql2, "CREATE TABLE %s打卡记录(date varchar(255),sign int);", s1.name);
        _sqlite3_exec(db, sql2);

        char sql8[128];
        sprintf(sql8, "INSERT INTO 员工8月工资 VALUES ('%s','0','0','0','0');", s1.name);
        _sqlite3_exec(db, sql8);
    }
    else if (strcmp(s1.position, "客户") == 0)
    {
        char sql[128];
        sprintf(sql, "INSERT INTO 客户钱包 VALUES ('%s','0','0');", s1.name);
        _sqlite3_exec(db, sql);

        char sql2[128];
        sprintf(sql2, "CREATE TABLE %s购买记录(卖家名字 varchar(255),产品 varchar(255));", s1.name);
        _sqlite3_exec(db, sql2);
    }

    char sql1[128];
    sprintf(sql1, "INSERT INTO zhanghao VALUES ('%s','%d','%s','%d');", s1.name, s1.passwd, s1.position, s1.fd);
    _sqlite3_exec(db, sql1);
    printf("%s注册成功\n", s1.name);
}
void _denlu(int fd)//登录
{
    dkbg();
    reg s1 = *(reg *)msg.buf;
    char sql1[128];
    sprintf(sql1, "UPDATE zhanghao SET fd = '%d'  WHERE name = '%s';", fd, s1.name);
    _sqlite3_exec(db, sql1);

    sprintf(sql1, "SELECT * FROM zhanghao WHERE name ='%s'and passwd='%d'", s1.name, s1.passwd);
    char **ppResult;
    int row;
    int col;
    sqlite3_get_table(db, sql1, &ppResult, &row, &col, &emsg);

    if (row != 0)
    {
        sprintf(sql1, "SELECT * FROM 员工离职申请 WHERE name ='%s'", s1.name);
        char **ppResult1;
        int row1;
        int col1;
        sqlite3_get_table(db, sql1, &ppResult1, &row1, &col1, &emsg);

        if (strcmp(ppResult1[col1 + 2], "0") == 0)
        {
            msg.type = 202;
            strcpy(s1.position, ppResult[col * row + col - 2]);
            s1.fd = atoi(ppResult[col * row + col - 1]);
            memcpy(msg.buf, &s1, sizeof(s1));
            write(fd, &msg, sizeof(msg));
            printf("%s登录失败\n", s1.name);
        }
        else
        {
            msg.type = 201;
            printf("%s登录成功\n", s1.name);

            strcpy(s1.position, ppResult[col * row + col - 2]);
            s1.fd = atoi(ppResult[col * row + col - 1]);

            memcpy(msg.buf, &s1, sizeof(s1));
            write(fd, &msg, sizeof(msg));
        }
    }
    if (row == 0)
    {
        msg.type = 203;
        printf("未识别到账号\n");
        write(fd, &msg, sizeof(msg));
    }
}
void logout(int fd)//客户注销
{
    dkbg();
    reg s1 = *(reg *)msg.buf;

    char sql2[512];
    sprintf(sql2, "SELECT * FROM zhanghao WHERE name = '%s';", s1.name);
    char **ppResult2;
    int row2;
    int col2;
    sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
    if (row2 == 0)
    {
        msg.type = 302;
        write(fd, &msg, sizeof(msg));
    }
    else
    {
        char s1l2[128];
        sprintf(s1l2, "DELETE FROM zhanghao WHERE name = '%s' and passwd = '%d';", s1.name, s1.passwd);
        _sqlite3_exec(db, s1l2);

        sprintf(sql2, "DROP TABLE %s钱包;", s1.name); // 暴力删除整个表格
        _sqlite3_exec(db, sql2);

        sprintf(sql2, "DROP TABLE %s购买记录;", s1.name); // 暴力删除整个表格
        _sqlite3_exec(db, sql2);

        printf("客户%s注销成功\n", s1.name);

        write(fd, &msg, sizeof(msg));
    }
}
void clockIn(int fd)//打卡
{
    dk d1 = *(dk *)msg.buf;
    d1.sign = 1;
    dkbg();

    char sql[512];
    sprintf(sql, "CREATE TABLE %s打卡记录 (date varchar(255),sign int);", d1.name);
    _sqlite3_exec(db, sql);

    char sql1[128];
    sprintf(sql1, "INSERT INTO %s打卡记录 VALUES ('%d月%d日','%d');", d1.name, d1.mouth, d1.day, d1.sign);
    _sqlite3_exec(db, sql1);
    printf("%s打卡成功\n", d1.name);
}
void Employeeleave(int fd)//员工请假
{
    dk d1 = *(dk *)msg.buf;
    dkbg();

    char sql2[128];
    sprintf(sql2, "INSERT INTO 员工请假 VALUES ('%s','%d月%d日','0');", d1.name, d1.mouth, d1.day);
    _sqlite3_exec(db, sql2);

    printf("%s申请成功\n", d1.name);
}
void permitleave(int fd)//批假系统
{
    char sql1[128];
    dkbg();
    int data;
    sprintf(sql1, "SELECT * FROM 员工请假 WHERE stat ='0'");
    char **ppResult;
    int row;
    int col;
    data = sqlite3_get_table(db, sql1, &ppResult, &row, &col, &emsg);
    if (data == 1)
    {
        printf("未找到stat=0的目标\n");
        msg.type = 602;
        write(fd, &msg, sizeof(msg));
        return;
    }
    pml d;
    for (int i = col; i < col * row + col; i++)
    {
        strcpy(d.name, ppResult[i]);
        printf("%s\n", d.name);
        strcpy(d.date, ppResult[++i]);
        printf("%s \n", d.date);
        d.stat = atoi(ppResult[++i]);
        printf("%d \n", d.stat);

        memcpy(msg.buf, &d, sizeof(d));
        write(fd, &msg, sizeof(msg));
        if (i == col * row + col - 1)
        {
            strcpy(msg.buf, "#");
            write(fd, &msg, sizeof(msg));
        }

        read(fd, &msg, sizeof(msg));
        pml d1 = *(pml *)msg.buf;
        char sql1[512];
        sprintf(sql1, "UPDATE 员工请假 SET stat = '%d'  WHERE date = '%s';", d1.stat, d1.date);
        _sqlite3_exec(db, sql1);

        if (d1.stat == 1) // 同意
        {
            char sql2[128];
            sprintf(sql2, "INSERT INTO %s打卡记录 VALUES ('%s','2');", d1.name, d1.date); // 请假在打卡表记2
            _sqlite3_exec(db, sql2);
        }

        if (d1.stat == 2) // 驳回
        {
            char sql2[128];
            sprintf(sql2, "UPDATE 员工请假 SET stat = '%d'  WHERE date = '%s';", d1.stat, d1.date);
            _sqlite3_exec(db, sql2);
        }
    }
}
void Viewprocess(int fd)//查看请假申请情况
{
    dk d1 = *(dk *)msg.buf;
    dkbg();

    char sql1[512];
    int data;
    sprintf(sql1, "SELECT * FROM 员工请假 WHERE name ='%s' and stat ='0'", d1.name); // 查看是否有未处理的请假请求
    char **ppResult;
    int row;
    int col;
    data = sqlite3_get_table(db, sql1, &ppResult, &row, &col, &emsg);
    printf("---row=%d---\n", row);
    if (row != 0)
    {
        if (strcmp(ppResult[col + 2], "0") == 0)
        {
            msg.type = 502;
            for (int i = col; i < col * row + col; i++)
            {

                pml p;
                strcpy(p.name, ppResult[i]);
                strcpy(p.date, ppResult[++i]);
                p.stat = atoi(ppResult[++i]);

                memcpy(msg.buf, &p, sizeof(p));
                write(fd, &msg, sizeof(msg));

                // if (i == col * row + col - 1)
                // {
                //     strcpy(msg.buf, "#");
                //     write(fd, &msg, sizeof(msg));
                // }
            }

            printf("%s的请假申请还未批准请等待\n", ppResult[col + 1]);
        }
    }

    char sql2[512];
    int data1;
    sprintf(sql1, "SELECT * FROM 员工请假 WHERE name ='%s' and stat ='1'", d1.name); // 查看已通过的请求
    char **ppResult1;
    int row1;
    int col1;
    sqlite3_get_table(db, sql1, &ppResult1, &row1, &col1, &emsg);
    printf("---row1=%d---\n", row1);

    if (row1 != 0)
    {
        if (strcmp(ppResult1[col1 + 2], "1") == 0)
        {
            msg.type = 503;
            for (int i = col1; i < col1 * row1 + col1; i++)
            {

                pml p;
                strcpy(p.name, ppResult1[i]);
                strcpy(p.date, ppResult1[++i]);
                p.stat = atoi(ppResult1[++i]);

                memcpy(msg.buf, &p, sizeof(p));
                write(fd, &msg, sizeof(msg));

                // if (i == col * row + col - 1)
                // {
                //     strcpy(msg.buf, "#");
                //     write(fd, &msg, sizeof(msg));
                // }
            }

            printf("%s的请假申请已批准\n", ppResult1[col1 + 1]);
        }
    }

    char sql3[512];
    int data2;
    sprintf(sql3, "SELECT * FROM 员工请假 WHERE name ='%s' and stat ='2'", d1.name); // 查看驳回的请求
    char **ppResult2;
    int row2;
    int col2;
    data2 = sqlite3_get_table(db, sql3, &ppResult2, &row2, &col2, &emsg);
    printf("---row2=%d---\n", row2);

    if (row2 == 0)
    {
        msg.type = 505;
        strcpy(msg.buf, "#");
        write(fd, &msg, sizeof(msg));
        printf("---222222---\n");
    }
    if (row2 != 0)
    {
        if (strcmp(ppResult2[col + 2], "2") == 0)
        {
            msg.type = 503;
            for (int i = col2; i < col2 * row2 + col2; i++)
            {

                pml p;
                strcpy(p.name, ppResult2[i]);
                strcpy(p.date, ppResult2[++i]);
                p.stat = atoi(ppResult2[++i]);

                memcpy(msg.buf, &p, sizeof(p));
                write(fd, &msg, sizeof(msg));

                if (i == col2 * row2 + col2 - 1)
                {
                    strcpy(msg.buf, "#");
                    write(fd, &msg, sizeof(msg));
                }
            }

            printf("%s的请假申请不予通过\n", ppResult2[col2 + 1]);
        }
    }
}
float dksum(char *p)//计算打卡次数
{
    dkbg();
    char sql3[512];
    int data2;
    float sum = 0;
    sprintf(sql3, "SELECT sign FROM %s打卡记录", p); // 无表格时会出现段错误
    char **ppResult2;
    int row2;
    int col2;
    data2 = sqlite3_get_table(db, sql3, &ppResult2, &row2, &col2, &emsg);
    for (int i = col2; i <= row2; i++)
    {
        if (strcmp(ppResult2[i], "1") == 0) // 出勤
        {
            sum += 1;
        }
        if (strcmp(ppResult2[i], "2") == 0) // 请假
        {
            sum += 0.5;
        }
    }
    return sum;
}
void Checksalary(int fd)//查看工资
{
    reg s1 = *(reg *)msg.buf;
    money m;
    dkbg();

    char sql3[512];
    int data2 = 2;
    sprintf(sql3, "SELECT * FROM 员工8月工资 WHERE name ='%s'", s1.name);
    char **ppResult2;
    int row2;
    int col2;
    data2 = sqlite3_get_table(db, sql3, &ppResult2, &row2, &col2, &emsg);
    if (row2 == 0) // 没找到时
    {
        msg.type = 702;
        write(fd, &msg, sizeof(msg));
    }

    if (row2 != 0) // 找到时
    {
        float dks = dksum(s1.name); // 打卡天数

        float yeji = atof(ppResult2[col2 + 1]); // 业绩

        float csum = atof(ppResult2[col2 + 2]); // 客户数量

        float wage = dks * 200 + yeji * 0.1; // 总工资
        wage = wage + wage * csum * 0.01;

        m.wage = wage;
        int mny = atoi(ppResult2[col2 + 4]);
        m.wallet = (float)mny; // 非指针强转可以不用*
        memcpy(msg.buf, &m, sizeof(m));
        write(fd, &msg, sizeof(msg));
    }
}
void paywages(int fd)//发工资
{
    dkbg();
    // 给员工发放工资条
    // 遍历每一个员工
    char sql[512];
    sprintf(sql, "SELECT * FROM zhanghao WHERE position = '员工'");
    char **ppResult;
    int row;
    int col;
    sqlite3_get_table(db, sql, &ppResult, &row, &col, &emsg);

    // 给每一个员工发工资条
    for (int i = col; i < col * row + col;)
    {
        char sql1[512];
        sprintf(sql1, "SELECT * FROM 员工8月工资 WHERE name = '%s'", ppResult[i]);
        char **ppResult1;
        int row1;
        int col1;
        sqlite3_get_table(db, sql1, &ppResult1, &row1, &col1, &emsg);
        printf("i=%d ppResult[i]=%s row1=%d\n", i, ppResult[i], row1);
        if (row1 != 0) // 找到则更新
        {
            float dks = dksum(ppResult[i]); // 打卡天数
            printf("ppResult1[col1]=%s\n", ppResult1[col1]);

            float yeji = atof(ppResult1[col1 + 1]); // 业绩

            float csum = atof(ppResult1[col1 + 2]); // 客户数量

            float wage = dks * 200 + yeji * 0.1; // 总工资
            wage = wage + wage * csum * 0.01;

            float p = atof(ppResult1[col1 + 3]);
            float money = p + wage; // 余额
            printf("p=%f wage=%f, dks=%f, yeji=%f,csum=%f\n", p, wage, dks, yeji, csum);
            char sql3[512];
            sprintf(sql3, "UPDATE 员工8月工资 SET balance = '%f',salary = '0',performance = '0',custmers = '0' WHERE name = '%s';", money, ppResult[i]);
            _sqlite3_exec(db, sql3);

            char sql4[512];
            sprintf(sql4, "DELETE FROM %s打卡记录 WHERE sign = '1';", ppResult[i]);
            _sqlite3_exec(db, sql4);

            char sql5[512];
            sprintf(sql5, "DELETE FROM %s打卡记录 WHERE sign = '1';", ppResult[i]);
            _sqlite3_exec(db, sql5);

            msg.type = 801;
            strcpy(msg.buf, ppResult[i]);
            // memcpy(msg.buf, &name, sizeof(name));
            write(fd, &msg, sizeof(msg));
        }
        if (i == col * row + col - 4)
        {
            strcpy(msg.buf, "#");
            write(fd, &msg, sizeof(msg));

            break;
        }
        i += 4;
    }
}
void Topup(int fd)//客户充值
{
    char name[50];
    strcpy(name, msg.buf);
    dkbg();

    char sql1[512];
    sprintf(sql1, "SELECT * FROM 客户钱包 WHERE name = '%s'", name);
    char **ppResult;
    int row;
    int col;
    sqlite3_get_table(db, sql1, &ppResult, &row, &col, &emsg);
    if (row != 0)
    {
        topup pt = *(topup *)msg.buf;

        float money = atof(ppResult[col + 1]);

        char sql3[512];
        sprintf(sql3, "UPDATE 客户钱包 SET balance = '%f' WHERE name = '%s';", money + pt.jine, name);
        _sqlite3_exec(db, sql3);

        pt.jine = money + pt.jine;
        memcpy(msg.buf, &pt, sizeof(pt));
        write(fd, &msg, sizeof(msg));
    }
}
void signbill(int fd)//员工录单
{
    sb s = *(sb *)msg.buf;

    char sql1[512];
    sprintf(sql1, "SELECT * FROM zhanghao WHERE name = '%s'", s.othername);
    char **ppResult;
    int row;
    int col;
    sqlite3_get_table(db, sql1, &ppResult, &row, &col, &emsg);
    if (row == 0)
    {
        msg.type = 1002;
        write(fd, &msg, sizeof(msg));
    }
    if (row != 0)
    {

        char sql[512];
        sprintf(sql, "SELECT * FROM 客户钱包 WHERE name = '%s'", s.othername);
        char **ppResult1;
        int row1;
        int col1;
        sqlite3_get_table(db, sql, &ppResult1, &row1, &col1, &emsg);

        cw c;
        c.fd = atoi(ppResult[col + 3]);

        // read(c.fd, &msg, sizeof(msg)); // 等待客户响应

        strcpy(c.name, ppResult1[col1]);
        c.balance = atof(ppResult1[col1 + 1]);
        c.jifen = atof(ppResult1[col1 + 2]);
        memcpy(msg.buf, &c, sizeof(c));
        write(c.fd, &msg, sizeof(msg));

        while (1)
        {
            int ret = read(c.fd, &msg, sizeof(msg));
            // 客户端强行退出时防止乱码
            if (ret == 0)
            {
                printf("%d已退出服务器\n", c.fd);

                epoll_ctl(epollfd, EPOLL_CTL_DEL, c.fd, &ev);
                break;
            }

            write(fd, &msg, sizeof(msg));
            if (strcmp(msg.buf, "#") == 0)
            {
                break;
            }

            mcg m1 = *(mcg *)msg.buf;

            // 写入购买记录
            char cpname[50];
            if (m1.cbalance == 3000)
            {
                strcpy(cpname, "电脑");
            }
            if (m1.cbalance == 2000)
            {
                strcpy(cpname, "手机");
            }
            if (m1.cbalance == 50)
            {
                strcpy(cpname, "鼠标");
            }
            if (m1.cbalance == 40)
            {
                strcpy(cpname, "键盘");
            }
            if (m1.cbalance == 100)
            {
                strcpy(cpname, "充电器");
            }

            char sql1[128];
            sprintf(sql1, "INSERT INTO %s购买记录 VALUES ('%s','%s');", s.othername, s.selfname, cpname);
            _sqlite3_exec(db, sql1);

            c.balance -= m1.cbalance;
            c.jifen += m1.cjifen;
            memcpy(msg.buf, &c, sizeof(c));
            write(c.fd, &msg, sizeof(msg));
            // 更新员工业绩和客户数
            char sql2[512];
            sprintf(sql2, "SELECT * FROM 员工8月工资 WHERE name = '%s'", s.selfname);
            char **ppResult2;
            int row2;
            int col2;
            sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);

            float oc = atof(ppResult2[col2 + 2]) + 1;
            float pp = atof(ppResult2[col2 + 1]) + m1.cbalance;

            char sql3[512];
            sprintf(sql3, "UPDATE 员工8月工资 SET performance = '%f',custmers = '%f' WHERE name = '%s';", pp, oc, s.selfname);
            _sqlite3_exec(db, sql3);
            // 更新客户钱包
            char sql4[512];
            sprintf(sql4, "UPDATE 客户钱包 SET balance = '%f',jifen = '%f' WHERE name = '%s';", c.balance, c.jifen, c.name);
            _sqlite3_exec(db, sql4);
        }
    }
}
void replenishment(int fd)//补卡
{
    char name[128];
    char sql2[512];
    strcpy(name, msg.buf);
    sprintf(sql2, "SELECT * FROM %s打卡记录", name);
    char **ppResult2;
    int row2;
    int col2;
    sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
    for (int i = col2; i < col2 * row2 + col2; i++)
    {
        pml p1;
        strcpy(p1.date, ppResult2[i]);
        strcpy(p1.name, name);
        p1.stat = atoi(ppResult2[++i]);

        memcpy(msg.buf, &p1, sizeof(p1));
        write(fd, &msg, sizeof(msg));
        printf("****ppResult2[i]=%s******\n", ppResult2[i]);
        if (i == col2 * row2 + col2 - 1)
        {
            strcpy(msg.buf, "#");
            write(fd, &msg, sizeof(msg));
            break;
        }
        printf("----msg.buf=%s----\n", msg.buf);
    }

    int ret = read(fd, &msg, sizeof(msg));
    if (ret == 0)
    {
        printf("%d已退出服务器\n", fd);

        epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &ev);
        char sql1[128];
        sprintf(sql1, "UPDATE zhanghao SET fd = '0'  WHERE fd = '%d';", fd);
        _sqlite3_exec(db, sql1);
        return;
    }
    pml p2 = *(pml *)msg.buf;

    char sql[512];
    sprintf(sql, "SELECT * FROM %s打卡记录 WHERE date = '%s'", p2.name, p2.date);
    char **ppResult;
    int row;
    int col;
    sqlite3_get_table(db, sql, &ppResult, &row, &col, &emsg);
    printf("---row=%d\n", row);
    if (row != 0)
    {
        if (p2.stat == 1)
        {
            char sql3[512];
            sprintf(sql3, "UPDATE %s打卡记录 SET sign = '1' WHERE date = '%s';", name, p2.date);
            _sqlite3_exec(db, sql3);
        }
        if (p2.stat == 2)
        {
            char sql3[512];
            sprintf(sql3, "UPDATE %s打卡记录 SET sign = '2' WHERE date = '%s';", name, p2.date);
            _sqlite3_exec(db, sql3);
        }
        if (p2.stat == 0)
        {
            char sql3[512];
            sprintf(sql3, "UPDATE %s打卡记录 SET sign = '0' WHERE date = '%s';", name, p2.date);
            _sqlite3_exec(db, sql3);
        }
    }
    if (row == 0)
    {
        char sql1[128];
        sprintf(sql1, "INSERT INTO %s打卡记录 VALUES ('%s','%d');", name, p2.date, p2.stat);
        _sqlite3_exec(db, sql1);
    }
    // write(fd, &msg, sizeof(msg));
}
void Viewperformance(int fd)//查看业绩
{
    char sql2[512];
    sprintf(sql2, "SELECT * FROM 员工8月工资 WHERE name = '%s'", msg.buf);
    char **ppResult2;
    int row2;
    int col2;
    sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
    if (row2 == 0)
    {
        msg.type = 1202;
        write(fd, &msg, sizeof(msg));
    }
    if (row2 != 0)
    {
        strcpy(msg.buf, ppResult2[col2 + 1]);
        write(fd, &msg, sizeof(msg));
    }
}
void dimissionl(int fd)//离职申请
{
    sb s = *(sb *)msg.buf;
    char sql[512];
    sprintf(sql, "CREATE TABLE 员工离职申请(name varchar(255),targetnmae varchar(255),state int);");
    _sqlite3_exec(db, sql);

    char sql1[128];
    sprintf(sql1, "INSERT INTO 员工离职申请 VALUES ('%s','%s','0');", s.selfname, s.othername);
    _sqlite3_exec(db, sql1);

    msg.type = 1301;
    write(fd, &msg, sizeof(msg));
}
void Approvalseparation(int fd)//审批离职申请
{
    char sql2[512];
    sprintf(sql2, "SELECT * FROM 员工离职申请 WHERE state = '0'");
    char **ppResult2;
    int row2;
    int col2;
    sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
    printf("row2=%d\n",row2);
    if (row2 == 0)
    {
        strcpy(msg.buf, "#");
        write(fd, &msg, sizeof(msg));
    }

    for (int i = col2; i < col2 * row2 + col2; i++)
    {
        sb s;
        strcpy(s.selfname, ppResult2[i]);
        strcpy(s.othername, ppResult2[++i]);
        memcpy(msg.buf, &s, sizeof(s));
        write(fd, &msg, sizeof(msg));

        read(fd, &msg, sizeof(msg));
        if (strcmp(msg.buf, "1") == 0)
        {
            char sql1[128];
            sprintf(sql1, "DELETE FROM zhanghao WHERE name = '%s';", s.selfname);
            _sqlite3_exec(db, sql1);

            char sql2[512];
            sprintf(sql2, "SELECT * FROM 员工8月工资 WHERE name = '%s'", s.selfname);
            char **ppResult2;
            int row2;
            int col2;
            sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
            float cus = atof(ppResult2[col2 + 2]);

            char sql3[512];
            sprintf(sql3, "UPDATE 员工8月工资 SET custmers=custmers+%f WHERE name = '%s';", cus, s.othername);
            _sqlite3_exec(db, sql3);

            sprintf(sql3, "UPDATE 员工离职申请 SET state ='1' WHERE name = '%s';", s.selfname);
            _sqlite3_exec(db, sql3);

            sprintf(sql1, "DELETE FROM 员工8月工资 WHERE name = '%s';", s.selfname);
            _sqlite3_exec(db, sql1);

            sprintf(sql3, "DROP TABLE %s打卡记录;", s.selfname); // 暴力删除整个表格
            _sqlite3_exec(db, sql3);
        }

        if (i == col2 * row2 + col2 - 2)
        {

            strcpy(msg.buf, "#");
            write(fd, &msg, sizeof(msg));
            break;
        }
    }
}
void Searchpurchasehistory(int fd)//查看历史购买记录
{
    char sql2[512];
    sprintf(sql2, "SELECT * FROM %s购买记录", msg.buf);
    char **ppResult2;
    int row2;
    int col2;
    sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
    printf("ppResult2[col2]=%s\n", ppResult2[col2]);
    for (int i = col2; i < col2 * row2 + col2; i++)
    {
        ph p1;
        strcpy(p1.name, ppResult2[i]);
        strcpy(p1.changpin, ppResult2[++i]);
        memcpy(msg.buf, &p1, sizeof(p1));
        write(fd, &msg, sizeof(msg));
        if (i == col2 * row2 + col2 - 1)
        {

            strcpy(msg.buf, "#");
            write(fd, &msg, sizeof(msg));
            break;
        }
    }
}
void creditsexchange(int fd)//积分兑换系统
{
    char sql2[512];
    sprintf(sql2, "SELECT * FROM 客户钱包 WHERE name = '%s'", msg.buf);
    char **ppResult2;
    int row2;
    int col2;
    sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
    float jifen = atof(ppResult2[col2 + 2]);

    sprintf(sql2, "UPDATE 客户钱包 SET jifen ='0',balance = balance+'%f' WHERE name = '%s';", jifen, msg.buf);
    _sqlite3_exec(db, sql2);

    strcpy(msg.buf, ppResult2[col2 + 2]);
    write(fd, &msg, sizeof(msg));
}
void privatechat(int fd)//私聊
{
    chat s = *(chat *)msg.buf;
    char sql2[512];
    sprintf(sql2, "SELECT * FROM zhanghao WHERE name = '%s'", s.othername);
    char **ppResult2;
    int row2;
    int col2;
    sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
    int cfd = atoi(ppResult2[col2 + 3]);
    printf("cfd=%d\n", cfd);
    if (cfd == 0)
    {
        msg.type = 1702; // 用户不在线
        write(fd, &msg, sizeof(msg));

        char sql1[128];
        sprintf(sql1, "INSERT INTO %s聊天记录 VALUES ('%s','%s','%s','1');", s.selfname, s.selfname, s.othername, s.test);
        _sqlite3_exec(db, sql1);

        sprintf(sql1, "INSERT INTO %s聊天记录 VALUES ('%s','%s','%s','0');", s.othername, s.othername, s.selfname, s.test);
        _sqlite3_exec(db, sql1);
    }
    if (cfd != 0)
    {
        char sql1[128];
        sprintf(sql1, "INSERT INTO %s聊天记录 VALUES ('%s','%s','%s','1');", s.selfname, s.selfname, s.othername, s.test);
        _sqlite3_exec(db, sql1);

        sprintf(sql1, "INSERT INTO %s聊天记录 VALUES ('%s','%s','%s','1');", s.othername, s.othername, s.selfname, s.test);
        _sqlite3_exec(db, sql1);

        write(cfd, &msg, sizeof(msg));
    }
}

void work(int fd)//留言查看
{
    char sql2[512];
    sprintf(sql2, "SELECT * FROM %s聊天记录 WHERE sign = '0'", msg.buf);
    char **ppResult2;
    int row2;
    int col2;
    sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
    chat s;
    msg.type = 1701; // 发送留言
    // printf("row2=%d\n",row2);
    if (row2 == 0)
    {

        strcpy(msg.buf, "#");
        write(fd, &msg, sizeof(msg));
    }
    else
    {
        for (int i = col2; i < col2 * row2 + col2; i++)
        {
            if (strcmp(ppResult2[col2 + 3], "0") == 0)
            {
                strcpy(s.selfname, ppResult2[i]);
                strcpy(s.othername, ppResult2[++i]);
                strcpy(s.test, ppResult2[++i]);
                int a = atoi(ppResult2[++i]);
                memcpy(msg.buf, &s, sizeof(s));

                write(fd, &msg, sizeof(msg));
                // printf("***s.test=%s***\n", s.test);

                char sql2[512];
                sprintf(sql2, "UPDATE %s聊天记录 SET sign = '1' WHERE txt = '%s';", s.selfname, s.test);
                _sqlite3_exec(db, sql2);
            }
            if (i == col2 * row2 + col2 - 1)
            {
                strcpy(msg.buf, "#");
                write(fd, &msg, sizeof(msg));
                break;
            }
        }
    }
}
void groupchat(int fd)//群聊
{
    chat s = *(chat *)msg.buf;
    char sql2[512];
    sprintf(sql2, "SELECT fd FROM zhanghao");
    char **ppResult2;
    int row2;
    int col2;
    sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
    // printf("row2=%d\n",row2);
    for (int i = col2; i < row2 * col2 + col2; i++)
    {
        int cfd = atoi(ppResult2[i]);
        if (strcmp(ppResult2[i], "0") == 0 | cfd == fd)
        {
            continue;
        }
        else
        {
            msg.type = 1801;
            write(cfd, &msg, sizeof(msg));
            printf("%s发送\n", s.selfname);
        }
    }
}
void Senddocument()//发文件
{
    sd s = *(sd *)msg.buf;
    char sql2[512];
    sprintf(sql2, "SELECT * FROM zhanghao WHERE name = '%s'", s.name);
    char **ppResult2;
    int row2;
    int col2;
    sqlite3_get_table(db, sql2, &ppResult2, &row2, &col2, &emsg);
    int cfd = atoi(ppResult2[col2 + 3]);
    printf("cfd=%d %s\n", cfd, s.buf);
    write(cfd, &msg, sizeof(msg));
}
int main(int argc, char *argv[])
{
    dkbg();
    char sql2[512];
    int a = 0;
    sprintf(sql2, "UPDATE zhanghao SET fd = '%d';", a);
    _sqlite3_exec(db, sql2);

    int fd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr = getaddr("192.168.50.176", 8000);
    int val = 1;
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
    bind(fd, (struct sockaddr *)&addr, sizeof(addr));
    listen(fd, 1000);

    epollfd = epoll_create1(0);

    if (epollfd == -1)
    {
        perror("epoll_create1");
    }
    ev.events = EPOLLIN; // 往结构体变量中放可读事件，按照逻辑第一个事件为监听
    ev.data.fd = fd;
    if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev))
        perror("epoll_ctl"); // 将监听事件放入epoll对象中，第三个参数是事件套接字

    while (1)
    {
        // 等待epoll对象中的套接字发生事件，并将发生事件放入结构体events中
        int nfd = epoll_wait(epollfd, events, 10, -1); // 用nfd记录发生事件的套接字总数量
        if (nfd == -1)
        {
            perror("epoll_wait");
        }
        for (int i = 0; i < nfd; i++)
        {
            if (events[i].data.fd == fd) // 如果发生事件的套接字数组中有监听套接字
            {
                struct sockaddr_in recaddr; // 创建新的接收结构体，接收客户端地址和端口
                socklen_t len = sizeof(recaddr);
                int acceptfd = accept(events[i].data.fd, (struct sockaddr *)&recaddr, &len); // 接收的同时产生新的套接字
                if (acceptfd == -1)
                {
                    perror("accept");
                }
                printf("%d已和服务器连接\n", acceptfd);
                ev.events = EPOLLIN;                              // 可读事件
                ev.data.fd = acceptfd;                            // 将套接字放入结构体
                epoll_ctl(epollfd, EPOLL_CTL_ADD, acceptfd, &ev); // 将事件套接字放入epoll对象
            }
            else
            {
                int ret = read(events[i].data.fd, &msg, sizeof(msg));
                // printf("ret=%d %d\n",ret,msg.type);
                if (ret == -1)
                {
                    perror("read");

                    ev.events = EPOLLIN;
                    ev.data.fd = events[i].data.fd;

                    epoll_ctl(epollfd, EPOLL_CTL_DEL, events[i].data.fd, &ev);
                }
                if (ret == 0)
                {
                    printf("%d已退出服务器\n", events[i].data.fd);

                    epoll_ctl(epollfd, EPOLL_CTL_DEL, events[i].data.fd, &ev);

                    char sql1[128];
                    sprintf(sql1, "UPDATE zhanghao SET fd = '0'  WHERE fd = '%d';", events[i].data.fd);
                    _sqlite3_exec(db, sql1);
                }
                else
                {
                    if (msg.type == 1) // 注册
                    {
                        _register(events[i].data.fd);
                    }
                    if (msg.type == 2) // 登录
                    {
                        _denlu(events[i].data.fd);
                    }
                    if (msg.type == 3) // 客户注销
                    {
                        logout(events[i].data.fd);
                    }
                    if (msg.type == 4) // 打卡
                    {
                        clockIn(events[i].data.fd);
                    }
                    if (msg.type == 5) // 请假
                    {
                        Employeeleave(events[i].data.fd);
                    }
                    if (msg.type == 501) // 请假
                    {
                        Viewprocess(events[i].data.fd);
                    }
                    if (msg.type == 6) // 批假
                    {
                        permitleave(events[i].data.fd);
                    }
                    if (msg.type == 7) // 查工资查余额
                    {
                        Checksalary(events[i].data.fd);
                    }
                    if (msg.type == 8) // 发放员工工资
                    {
                        paywages(events[i].data.fd);
                    }
                    if (msg.type == 9) // 客户充值系统
                    {
                        Topup(events[i].data.fd);
                    }
                    if (msg.type == 10) // 员工录单
                    {
                        signbill(events[i].data.fd);
                    }
                    if (msg.type == 11) // 补卡系统
                    {
                        replenishment(events[i].data.fd);
                    }
                    if (msg.type == 12) // 查看业绩
                    {
                        Viewperformance(events[i].data.fd);
                    }
                    if (msg.type == 13) // 员工离职申请
                    {
                        dimissionl(events[i].data.fd);
                    }
                    if (msg.type == 14) // 审批离职申请
                    {
                        Approvalseparation(events[i].data.fd);
                    }
                    if (msg.type == 15) // 购买记录
                    {
                        Searchpurchasehistory(events[i].data.fd);
                    }
                    if (msg.type == 16) // 积分兑换系统
                    {
                        creditsexchange(events[i].data.fd);
                    }
                    if (msg.type == 17) // 私聊
                    {
                        privatechat(events[i].data.fd);
                    }
                    if (msg.type == 111) // 后台交互
                    {
                        work(events[i].data.fd);
                    }
                    if (msg.type == 18) // 群聊
                    {
                        groupchat(events[i].data.fd);
                    }
                    if (msg.type == 19) // 传文件
                    {
                        Senddocument(events[i].data.fd);
                    }
                }
            }
        }
    }
}